<?php 
/*
 module:		会员管理控制器
 create_time:	2022-08-01 22:38:14
 author:		
 contact:		
*/

namespace app\Controller\admin;
use App\Exception\ValidateException;
use App\Model\admin\MemberModel;
use Hyperf\DbConnection\Db;

class MemberController extends CommonController {


	/*
 	* @Description  数据列表
 	*/
	function index(){
		if($this->request->isMethod('get')){
			return view('member/index');
		}else{
			$limit  = $this->request->post('limit', 20);
			$page = $this->request->post('page', 1);

			$where = [];
			$where['member_id'] = $this->request->post('member_id');
			$where['username'] = $this->request->post('username');
			$where['sex'] = $this->request->post('sex');

			$field = 'member_id,username,password,sex';

			$order  = $this->request->post('order');	//排序字段
			$sort  = $this->request->post('sort');		//排序方式

			$orderby = ($sort && $order) ? $sort.' '.$order : 'member_id desc';

			$res = MemberModel::where(formatWhere($where))->field($field)->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

			$data['status'] = 200;
			$data['data'] = $res;
			return json($data);
		}
	}


	/*
 	* @Description  修改排序开关
 	*/
	function updateExt(){
		$postField = 'member_id,';
		$data = $this->request->inputs(explode(',',$postField),'post');
		if(!$data['member_id']) throw new ValidateException ('参数错误');
		MemberModel::update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}

	/*
 	* @Description  添加
 	*/
	public function add(){
		$postField = 'username,password,sex';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\MemberValidate;
		if(!$validate->scene('add')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		$data['password'] = md5($data['password'].config('my.password_secrect'));

		try{
			$res = MemberModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/*
 	* @Description  修改
 	*/
	public function update(){
		$postField = 'member_id,username,sex';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\MemberValidate;
		if(!$validate->scene('update')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		try{
			MemberModel::update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	function getUpdateInfo(){
		$id =  $this->request->post('member_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'member_id,username,sex';
		$res = MemberModel::field($field)->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  删除
 	*/
	function delete(){
		$idx =  $this->request->post('member_id');
		if(!$idx) throw new ValidateException ('参数错误');
		MemberModel::destroy(['member_id'=>explode(',',$idx)],true);
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/*
 	* @Description  查看详情
 	*/
	function detail(){
		$id =  $this->request->post('member_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'member_id,username,password,sex';
		$res = MemberModel::field($field)->find($id);
		return json(['status'=>200,'data'=>$res]);
	}




}

